package gw.com.android.ui.quote2;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import com.gwtsz.chart.output.utils.ChartRendererFactory;
import com.gwtsz.chart.output.utils.Periodicity;

import www.com.library.app.AppActivities;
import www.com.library.util.DeviceUtil;

/**
 * 图表配置类
 * Created by reeta.zou on 2016/10/17.
 */
public class QuoteChartConfig {

    /**
     * k线类型,分时，1分钟线-月线
     */
    public final static int CHART_TYPE_TIME = 1;
    public final static int CHART_PERIOD_1_MINS = 2;
    public final static int CHART_PERIOD_5_MINS = 3;
    public final static int CHART_PERIOD_15_MINS = 4;
    public final static int CHART_PERIOD_30_MINS = 5;
    public final static int CHART_PERIOD_60_MINS = 6;
    public final static int CHART_PERIOD_2_HOURS = 7;
    public final static int CHART_PERIOD_4_HOURS = 8;
    public final static int CHART_PERIOD_DAY = 9;
    public final static int CHART_PERIOD_WEEK = 10;
    public final static int CHART_PERIOD_MONTH = 11;

    public static final int LONDON_GOLD = 3289904;      // 伦敦金
    public static final int LONDON_SILVER = 3355440;      // 伦敦银
    public static final int LIAN_GOLD = 3420720;      // 离岸人民币黄金
    public static final int LIAN_SILVER = 3486256;      // 离岸人民币白银
    /**
     * 明细v
     **/
    public final static int CHART_TYPE_DETAILED = 12;

    /**
     * 记录横竖屏方向
     **/
    public int mScreenOrientation = Configuration.ORIENTATION_PORTRAIT;

    /*** 图表中渲染器类型 */
    public static final String UPREF_CHART_RENDERER_TYPE = "ChartRendererType";
    /*** 图表中时间轴规模 */
    public static final String UPREF_CHART_TIMELINE_SCALE = "ChartTimelineScale";
    /**
     * 绘制图表用到的时间，类型等数据对象
     */
    public static final String UPREF_CHART_PERIODICITY = "ChartPeriodicity";
    /**
     * 图表中图表类型
     */
    public static final String UPREF_CHART_TYPE = "ChartType";
    /**
     * 当前主图类型，默认MA
     */
    public static final String UPREF_CHART_MAIN_K_TYPE = "ChartMainKType";
    /**
     * 当前副图类型，默认MACD
     */
    public static final String UPREF_CHART_SECOND_K_TYPE = "ChartSecondKType";
    public static final String SECOND_CHART_ON = "secondChartOnisopen"; // 是否开启副图指标

    public static final String CURRENT_QUOTE_CHART_PRODUCT = "CurrentQuoteChartProduct";
    public static final String LAST_SUCCESS_ORDER = "LastSuccessOrder";

    private String PREFERENCE_NAME = "quote_chart";
    /**
     * 用户参数保存对象
     */
    private SharedPreferences mPreferences;
    /*** 图表中渲染器类型 */
    private ChartRendererFactory.ChartRendererType mChartRendererType;
    /*** 图表中时间轴规模 */
    //private TimelineScale mChartTimelineScale;
    /**
     * 绘制图表用到的时间，类型等数据对象
     */
    private Periodicity mChartPeriodicity;
    /**
     * 图表中图表类型,是分时线还是k线
     */
    private int mChartType;
    /**
     * 当前主图类型，默认MA
     */
    private String mMainKType;
    /**
     * 当前副图类型，默认MACD
     */
    private String mSecondKType;

    /**
     * 图表类实例
     **/
    private static QuoteChartConfig mInstance;

    public static QuoteChartConfig instance() {
        if (null == mInstance) {
            mInstance = new QuoteChartConfig();
        }
        return mInstance;
    }

    public static float getDensity() {
        try {
            Activity activity = AppActivities.getSingleton().currentActivity();
            if (null != activity) {
                final float density = activity.getResources().getDisplayMetrics().density;
                return density;
            }
        } catch (Exception e) {
            return 1;
        }
        return DeviceUtil.instance().getScreenDensity(AppActivities.getSingleton().currentActivity());
    }

    /**
     * dp转换成px
     *
     * @param res
     * @param dipValue
     * @return
     */
    public static int getPixelFromDip(Resources res, float dipValue) {
        return getPixel(res, TypedValue.COMPLEX_UNIT_DIP, dipValue);
    }

    /**
     * @param res
     * @param unit
     * @param size
     * @return
     */
    public static int getPixel(Resources res, int unit, float size) {
        DisplayMetrics metrics = res.getDisplayMetrics();
        return (int) TypedValue.applyDimension(unit, size, metrics);
    }

    /**
     * sp转成px
     *
     * @param res
     * @param spValue
     * @return
     */
    public static int getPixelFromSP(Resources res, float spValue) {
        return getPixel(res, TypedValue.COMPLEX_UNIT_SP, spValue);
    }

    /**
     * @param res
     * @return
     */
    public static float getPreferredTextScaleX(Resources res) {
        final int wpx = res.getDisplayMetrics().widthPixels;
        if (wpx <= 320)
            return 0.5f;
        return 1.0f;
    }

    /**
     * 将px值转换为dip或dp值，保证尺寸大小不变
     *
     * @param pxValue （DisplayMetrics类中属性density）
     * @return
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 将dip或dp值转换为px值，保证尺寸大小不变
     *
     * @param dipValue （DisplayMetrics类中属性density）
     * @return
     */
    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    /**
     * 将px值转换为sp值，保证文字大小不变
     *
     * @param pxValue （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     *
     * @param spValue （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public QuoteChartConfig() {
        mPreferences = null;
    }

    /**
     * 检查图表参数对象
     *
     * @param context
     * @param mCurName
     */
    public void checkPreference(Context context, String mCurName) {
        if (null == mPreferences) {
            readPrefrence(context, mCurName);
        }
    }

    /**
     * 初始化图表参数保存对象
     *
     * @param mCurName
     */
    public void readPrefrence(Context context, String mCurName) {
        if (mCurName == null || mCurName.isEmpty())
            mPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        else
            mPreferences = context.getSharedPreferences(PREFERENCE_NAME + mCurName, Context.MODE_PRIVATE);

        // 图表渲染器类型, 蜡烛，棒型，线性
        int chartRendererType = mPreferences.getInt(UPREF_CHART_RENDERER_TYPE, -1);
        /*if (chartRendererType < 0) {
         *//* Default renderer type *//*
            mChartRendererType = ChartRendererFactory.ChartRendererType.CANDLE_STICK;
        } else {
            mChartRendererType = ChartRendererFactory.ChartRendererType.getType(chartRendererType);
        }*/

        // 图表时间轴规模（大小）
        int chartTimelineScale = mPreferences.getInt(UPREF_CHART_TIMELINE_SCALE, -1);
        /*if (chartTimelineScale < 0) {
            mChartTimelineScale = TimelineScale.BRG04;
        } else {
            mChartTimelineScale = TimelineScale.getScale(chartTimelineScale);
        }*/

        // 图表周期
        int chartPeriodicity = mPreferences.getInt(UPREF_CHART_PERIODICITY, -1);
        if (chartPeriodicity < 0) {
            mChartPeriodicity = Periodicity.getDefault();
        } else {
            mChartPeriodicity = Periodicity.nativeTypeToPeriodicity(chartPeriodicity);
        }

        // 图表类型，分时，k线等周期，明细
        mChartType = mPreferences.getInt(UPREF_CHART_TYPE, CHART_TYPE_TIME);

        // 图表主图类型，默认为MA 2
        mMainKType = mPreferences.getString(UPREF_CHART_MAIN_K_TYPE, "MA");

        // 图表副图类型，默认为MACD 8
        mSecondKType = mPreferences.getString(UPREF_CHART_SECOND_K_TYPE, "MACD");
    }

    /**
     * 返回是否为竖屏标识
     *
     * @return
     */
    public boolean isScreenPort() {
        if (mScreenOrientation == Configuration.ORIENTATION_PORTRAIT) {
            return true;
        }
        return false;
    }

    /**
     * 返回k线类型
     *
     * @return
     */
    public ChartRendererFactory.ChartRendererType getChartRendererType() {
        int chartRendererType = mPreferences.getInt(UPREF_CHART_RENDERER_TYPE, -1);
        if (chartRendererType < 0) {
            mChartRendererType = ChartRendererFactory.ChartRendererType.CANDLE_STICK;
        } else {
            mChartRendererType = ChartRendererFactory.ChartRendererType.getType(chartRendererType);
        }
        return mChartRendererType;
    }

    /**
     * 设置k线类型
     *
     * @param type
     */
    public void setChartRendererType(ChartRendererFactory.ChartRendererType type) {
        mChartRendererType = type;
    }

    /**
     * 保存k线类型
     */
    public void saveChartRendererType() {
        Editor editor = mPreferences.edit();
        editor.putInt(UPREF_CHART_RENDERER_TYPE, mChartRendererType.typeId());
        editor.apply();
    }

    /**
     * 返回时间周期规格
     *
     * @return
     */
    /*public TimelineScale getChartTimelineScale() {
        return mChartTimelineScale;
    }*/

    /**
     * 设置时间周期规格
     *
     * @param scale
     */
    /*public void setChartTimelineScale(TimelineScale scale) {
        mChartTimelineScale = scale;
    }*/

    /**
     * 保存时间周期规格
     */
    /*public void saveChartTimelineScale() {
        Editor editor = mPreferences.edit();
        editor.putInt(UPREF_CHART_TIMELINE_SCALE, mChartTimelineScale.barsPerGrid());
        editor.apply();
    }*/

    /**
     * 设置图表类型
     *
     * @param type
     */
    public void setChartType(int type) {
        mChartType = type;
    }

    /**
     * 返回图表类型
     *
     * @return
     */
    public int getChartType() {
        return mChartType;
    }

    /**
     * 保存图表类型
     */
    public void saveChartType() {
        Editor editor = mPreferences.edit();
        editor.putInt(UPREF_CHART_TYPE, mChartType);
        editor.apply();
    }

    /**
     * 设置图表主图类型
     *
     * @param type
     */
    public void setMainKType(String type) {
        mMainKType = type;
    }

    /**
     * 返回图表主图类型
     *
     * @return
     */
    public String getMainKType() {
        return mMainKType;
    }

    /**
     * 保存图表主图类型
     */
    public void saveMainKType() {
        Editor editor = mPreferences.edit();
        editor.putString(UPREF_CHART_MAIN_K_TYPE, mMainKType);
        editor.apply();
    }

    /**
     * 设置图表副图类型
     *
     * @param type
     */
    public void setSecondKType(String type) {
        mSecondKType = type;
    }

    /**
     * 返回图表副图类型
     *
     * @return
     */
    public String getSecondKType() {
        return mSecondKType;
    }

    /**
     * 保存图表副图类型
     */
    public void saveSecondKType() {
        Editor editor = mPreferences.edit();
        editor.putString(UPREF_CHART_SECOND_K_TYPE, mSecondKType);
        editor.apply();
    }

    /**
     * 返回周期对象
     *
     * @return
     */
    public Periodicity getChartPeriodicity() {
        return mChartPeriodicity;
    }

    /**
     * 设置周期对象
     *
     * @param period
     */
    public void setChartPeriodicity(Periodicity period) {
        mChartPeriodicity = period;
    }

    /**
     * 保存周期对象
     */
    public void saveChartPeriodicity() {
        Editor editor = mPreferences.edit();
        editor.putInt(UPREF_CHART_PERIODICITY, mChartPeriodicity.nativeKLineType());
        editor.apply();
    }

    /**
     * 是否开启副图指标显示
     */
    public boolean isSecondChartOn() {
        return mPreferences.getBoolean(SECOND_CHART_ON, true);

    }

    /**
     * 设置开启副图指标显示的唯一标识
     */
    public void saveSecondChartOn(boolean isKeep) {
        Editor editor = mPreferences.edit();
        editor.putBoolean(SECOND_CHART_ON, isKeep);

        editor.apply();
    }

    public void saveLastTime(String key, String time) {
        Editor editor = mPreferences.edit();
        editor.putString(key, time);
        editor.apply();
    }

    public String getLastTime(String key) {
        return mPreferences.getString(key, "");
    }

    public void setQuoteChartCurrentProduct(int uiCode) {
        Editor editor = mPreferences.edit();
        editor.putInt(CURRENT_QUOTE_CHART_PRODUCT, uiCode);
        editor.apply();
    }

    public int getQuoteChartCurrentProduct() {
        return mPreferences.getInt(CURRENT_QUOTE_CHART_PRODUCT, -1);
    }
}
